package org.ycx.doc.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.ycx.doc.dao.auto.DocGroupMapper;
import org.ycx.doc.dao.model.DocGroup;
import org.ycx.doc.dao.model.DocGroupUser;
import org.ycx.doc.dto.DocUser;

import java.util.List;

/**
 * DocGroup 自定义Mapper
 * Created by yang on 2017-08-30 17:02:41.
 */
public interface IDocGroupMapper extends DocGroupMapper {


    @Results({
            @Result(column = "GROUP_ID", property = "groupId", jdbcType = JdbcType.INTEGER, id = true),
            @Result(column = "GROUP_NAME", property = "groupName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "GROUP_DESC", property = "groupDesc", jdbcType = JdbcType.VARCHAR),
            @Result(column = "CREATE_TS", property = "createTs", jdbcType = JdbcType.DATE),
            @Result(column = "CREATE_UID", property = "createUid", jdbcType = JdbcType.INTEGER),
            @Result(column = "UPDATE_TS", property = "updateTs", jdbcType = JdbcType.DATE),
            @Result(column = "UPDATE_UID", property = "updateUid", jdbcType = JdbcType.INTEGER)
    })
    @Select("select dg.* from doc_group dg,doc_group_user dgu where dgu.GROUP_ID = dg.GROUP_ID and dgu.USER_ID=#{uid} and dgu.role=#{type}")
    public List<DocGroup> findDocGroupByUser(@Param("uid") Integer uid, @Param("type") Integer type);


    @Insert("insert into doc_group_user(GROUP_ID,USER_ID,ROLE)values(#{groupId},#{userId},#{role})")
    int insertGroupUser(DocGroupUser record);

    @Insert("insert into doc_group_project(GROUP_ID,APP_ID) values(#{gid},#{appId})")
    int insertGroupProject(@Param("gid") Integer gid, @Param("appId") Integer appId);

    @Select("select role from doc_group_user where GROUP_ID = #{groupId} and USER_ID=#{uid}")
    Integer findGroupUserRole(@Param("groupId") Integer groupId, @Param("uid") Integer uid);

    @Results({
            @Result(column="UID", property="uid", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="LOGIN_NAME", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="PASSWD", property="passwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="NICK_NAME", property="nickName", jdbcType=JdbcType.VARCHAR),
            @Result(column="GENDER", property="gender", jdbcType=JdbcType.VARCHAR),
            @Result(column="MOBILE", property="mobile", jdbcType=JdbcType.VARCHAR),
            @Result(column="EMAIL", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="AVATER", property="avater", jdbcType=JdbcType.VARCHAR),
            @Result(column="PWD_END_DATE", property="pwdEndDate", jdbcType=JdbcType.DATE),
            @Result(column="USE_STATE", property="useState", jdbcType=JdbcType.VARCHAR),
            @Result(column="DEL_STATE", property="delState", jdbcType=JdbcType.VARCHAR),
            @Result(column="CREATE_TS", property="createTs", jdbcType=JdbcType.DATE),
            @Result(column="CREATE_UID", property="createUid", jdbcType=JdbcType.INTEGER),
            @Result(column="UPDATE_TS", property="updateTs", jdbcType=JdbcType.DATE),
            @Result(column="UPDATE_UID", property="updateUid", jdbcType=JdbcType.INTEGER)
    })
    @Select("SELECT su.*,dgu.ROLE,dgu.GROUP_ID gid from doc_group_user dgu,sec_user su where dgu.USER_ID=su.UID and dgu.GROUP_ID=#{gid} order by dgu.role")
    List<DocUser> findGroupUser(@Param("gid") Integer gid);

    @Results({
            @Result(column="UID", property="uid", jdbcType=JdbcType.INTEGER, id=true),
            @Result(column="LOGIN_NAME", property="loginName", jdbcType=JdbcType.VARCHAR),
            @Result(column="PASSWD", property="passwd", jdbcType=JdbcType.VARCHAR),
            @Result(column="NICK_NAME", property="nickName", jdbcType=JdbcType.VARCHAR),
            @Result(column="GENDER", property="gender", jdbcType=JdbcType.VARCHAR),
            @Result(column="MOBILE", property="mobile", jdbcType=JdbcType.VARCHAR),
            @Result(column="EMAIL", property="email", jdbcType=JdbcType.VARCHAR),
            @Result(column="AVATER", property="avater", jdbcType=JdbcType.VARCHAR),
            @Result(column="PWD_END_DATE", property="pwdEndDate", jdbcType=JdbcType.DATE),
            @Result(column="USE_STATE", property="useState", jdbcType=JdbcType.VARCHAR),
            @Result(column="DEL_STATE", property="delState", jdbcType=JdbcType.VARCHAR),
            @Result(column="CREATE_TS", property="createTs", jdbcType=JdbcType.DATE),
            @Result(column="CREATE_UID", property="createUid", jdbcType=JdbcType.INTEGER),
            @Result(column="UPDATE_TS", property="updateTs", jdbcType=JdbcType.DATE),
            @Result(column="UPDATE_UID", property="updateUid", jdbcType=JdbcType.INTEGER)
    })
    @Select ("select * from sec_user where LOGIN_NAME=#{user}")
    List<DocUser> findUserByLoginName(@Param ("user") String user);

    @Insert ("insert into doc_group_user(GROUP_ID,USER_ID,ROLE) values(#{gid},#{uid},#{role})")
    int addGroupUser(@Param ("uid") Integer uid, @Param ("gid") Integer gid, @Param ("role") Integer role);

    @Delete ("delete from doc_group_user where group_id=#{gid} and user_id=#{uid}")
    int deleteGroupUser(@Param ("gid") Integer gid, @Param ("uid") Integer uid);

    @Select ("select count(*) from doc_group_user where group_id=#{gid} and role= #{type}")
    int countGroupUserByRole(@Param ("gid") Integer gid, @Param ("type") int type);

    @Select ("select su.*,dgu.role from doc_group_user dgu,sec_user su where su.UID = dgu.USER_ID and su.uid=#{uid} and dgu.GROUP_ID=#{gid} ")
    DocUser findGroupUserById(@Param("gid") Integer gid, @Param ("uid") Integer uid);
}